package com.blyat.xsoft.kernel.component.mybatis.dialect;

import com.google.common.collect.Maps;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * @author syh
 * @date 2019/10/15
 **/
public class DialectFactory {

    private static final Logger LOGGER = LoggerFactory.getLogger(DialectFactory.class);
    private static final Map<DATABASE, Dialect> DATABASEMAP =Maps.newConcurrentMap();

    public static Dialect build(String code) {
        DATABASE database = DATABASE.valueOf(code.toUpperCase());
        if (DATABASEMAP.containsKey(database)) {
            return DATABASEMAP.get(database);
        }
        LOGGER.info("已选择的数据库类型：{}", code);
        Dialect dialect;
        switch (database) {
            case MYSQL:
                dialect = new MySQLDialect();
                break;
            case ORACLE:
                dialect = new OracleDialect();
                break;
            default:
                throw new RuntimeException("不支持的数据库类型。");
        }
        DATABASEMAP.put(database, dialect);
        return dialect;
    }

    public enum DATABASE {
        MYSQL, ORACLE
    }
}
